{
  "cells": [
    {
      "cell_type": "markdown",
      "id": "e1957cca",
      "metadata": {},
      "source": [
        "基础操作：工作区 / 暂存区 / 本地库流转（add/commit/push）\n",
        "分支管理：GitFlow 流程（feature/dev/main 分支设计）\n",
        "面试重点：\n",
        "冲突解决的完整步骤（三向合并原理）\n",
        "reset 与 revert 的区别及适用场景\n",
        "如何回滚远程已提交的错误版本1\n",
        "实战场景：多人协作时的代码评审（PR 流程）"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "074a4858",
      "metadata": {},
      "source": [
        "## 理论\n",
        "- 冲突解决的完整步骤（三向合并原理）\n",
        "三向合并是 Git 解决分支冲突的核心机制，通过比较三个版本实现：\n",
        "\n",
        "共同祖先版本（BASE）：两个分支最近的共同提交\n",
        "当前分支版本（OURS）：当前所在分支的修改\n",
        "目标分支版本（THEIRS）：待合并分支的修改\n",
        "\n",
        "- reset 与 revert 的区别及适用场景\n",
        "reset:直接移动分支指针，丢弃指定提交后的历史,改写提交历史（不可逆） ,远程分支需强制推送（-f），风险高，适用于本地未推送的错误提交\n",
        "revert：创建新提交抵消指定提交的修改，保留历史，不改变原有历史，新增抵消提交，远程分支可正常推送，安全，适用于已推送到远程的错误提交\n",
        "\n",
        "- 如何回滚远程已提交的错误版本1\n",
        "使用 git revert（推荐，安全）\n",
        "\n"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "c56f8e13",
      "metadata": {},
      "source": [
        "## 基本语法"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "24c1a034",
      "metadata": {
        "vscode": {
          "languageId": "shellscript"
        }
      },
      "outputs": [],
      "source": [
        "\n",
        "# 显示 git 日志\n",
        "git log --oneline --graph --all\n",
        "\n"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "a05a0a96",
      "metadata": {},
      "source": [
        "\n",
        "git 规则：\n",
        "- 主分支（main）：禁止直接推送，只能通过 PR 合并\n",
        "- 开发分支（develop）：禁止直接推送，只能通过 PR 合并\n",
        "- 要求代码评审和 CI 检查通过才能合并"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "92df0cd7",
      "metadata": {},
      "source": []
    },
    {
      "cell_type": "markdown",
      "id": "3ba92468",
      "metadata": {},
      "source": [
        "## 场景实战"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "8c651edf",
      "metadata": {
        "vscode": {
          "languageId": "shellscript"
        }
      },
      "outputs": [],
      "source": [
        "cd /Users/mac/pspace/gitee\n",
        "git clone git@gitee.com:EngineMonkey/gitdemo.git\n",
        "\n",
        "# 1. 功能开发流程\n",
        "# 确保本地开发分支是最新的\n",
        "git checkout develop\n",
        "git pull origin develop\n",
        "\n",
        "# 创建功能分支（从 develop 分出）\n",
        "git checkout -b feature/user-authentication\n",
        "\n",
        "# 进行开发，频繁提交有意义的变更,添加到暂存区后，想移除 \"git restore --staged <file>...\n",
        "git add .               \n",
        "# git commit 之后 可以撤回 \n",
        "# 撤回方式1:git revert <commit id>HEAD  如果你想要撤销某个特定的提交，但又不想改变项目的历史记录（即创建一个新的提交来回滚之前的提交），会\n",
        "# (推荐)撤回方式2:git reset --soft HEAD~1 如果你只想撤销最后一次提交，同时保留工作目录中的更改\n",
        "# 撤回方式3:git reset --hard HEAD~1 如果你想要撤销最后一次提交，并且丢弃工作目录中的更改：\n",
        "\n",
        "git commit -m \"feat: 实现用户登录表单\"\n",
        "\n",
        "# 定期同步 develop 最新代码（避免后期合并冲突）\n",
        "git fetch origin\n",
        "git merge origin/develop\n",
        "\n",
        "# 开发完成后推送到远程\n",
        "git push -u origin feature/user-authentication\n",
        "\n",
        "\n",
        "\n",
        "# 2. 版本发布流程\n",
        "# 从开发分支创建发布分支\n",
        "git checkout develop\n",
        "git pull origin develop\n",
        "git checkout -b release/v1.2.0\n",
        "\n",
        "# 仅进行 bug 修复和文档更新\n",
        "git commit -m \"fix: 修复登录验证逻辑错误\"\n",
        "\n",
        "# 完成后推送到远程\n",
        "git push -u origin release/v1.2.0\n",
        "\n",
        "#创建两个 PR：\n",
        "#合并 release/v1.2.0 到 master\n",
        "#同时合并 release/v1.2.0 到 develop（确保开发分支包含修复）\n",
        "# 3、合并后在 master 分支打标签：\n",
        "git checkout master\n",
        "git pull origin master\n",
        "git tag -a v1.2.0 -m \"Version 1.2.0\"\n",
        "git push origin v1.2.0\n",
        "# 最后删除 release 分支。\n",
        "\n",
        "#4、生产环境紧急修复流程\n",
        "# 从主分支创建热修复分支\n",
        "git checkout master\n",
        "git pull origin master\n",
        "git checkout -b hotfix/payment-security-fix\n",
        "\n",
        "# 修复问题并提交\n",
        "git commit -m \"fix: 修复支付漏洞问题\"\n",
        "\n",
        "# 推送到远程\n",
        "git push -u origin hotfix/payment-security-fix\n",
        "\n",
        "\n",
        "#创建两个 PR：\n",
        "#合并 hotfix/payment-security-fix 到 main\n",
        "#同时合并 hotfix/payment-security-fix 到 develop\n",
        "\n",
        "#合并后在 main 分支打标签：\n",
        "git tag -a v1.1.1 -m \"Version 1.1.1\"\n",
        "git push origin v1.1.1\n",
        "\n",
        "#最后删除 hotfix 分支。\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "660f7b73",
      "metadata": {
        "vscode": {
          "languageId": "shellscript"
        }
      },
      "outputs": [],
      "source": [
        "# 首先查看是否存在名为 feature 的分支：\n",
        "git branch -r | grep feature\n",
        "\n",
        "# 删除远程 feature 分支\n",
        "git push origin --delete feature\n",
        "\n",
        "# 同步本地分支信息\n",
        "git fetch --prune"
      ]
    }
  ],
  "metadata": {
    "language_info": {
      "name": "python"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 5
}
